Queries in QGIS pt2 – Spatial Query
In the previous post (Queries in QGIS pt 1 – Attribute Queries), I showed how to select features based on their attribute data, e.g. select shops which have the address entered as London. Now let’s imagine there isn’t an address attribute, or alternatively, we want to select features within flood plains for example, or identify areas of woodland that have high voltage power lines running through them.
Spatial selection selects features in one layer based on their spatial relationship to features in another layer
To begin a spatial query either:
- Use the menu to select Vector, Spatial Query
- Press the Spatial Query button
The spatial query dialogue box:
Select Source Features From – this is the table that the selection will be from.
Where the feature – the options will change depending on the exact combination of point, line or region features that are being used for the selection. They include contains (e.g. a region layer may contain points), crosses (e.g. a line layer may cross a region layer), Is Within (e.g. a point layer may be within a region layer), Touches (where one object may touch another but not actually be within it).
Reference Features Of – this is the second layer. It will be used to select features from the first layer, but its features won’t be part of the selection
The selected geometries only will only use features that have already been selected. For example if I wanted to select all the woodland within a particular county I could set up the query to read
- Select Source Features from Farmland
- Where the Feature is Within
- Reference features of County Boundaries (selected features only)
Click OK, the query will run. Once it has completed, the results will appear:-
It is possible to run a further query based on the selection
Press the Create Layer from Selected to add the selection to the map as a new layer:-
Combined Spatial and Attribute Query
Quite often GIS is used to select features that contain a certain attribute within a certain area (e.g. all the A roads and motorways within Greater London).
These queries are carried out by combining the above Spatial and attribute selections. They can be carried out either order depending on which is most logical.
In this example I would overlay the roads and OS Boundary Line features. I would then select all the London Boroughs by clicking on them with the Select Single Feature tool
- Select Source Features from Roads
- Where the Feature is Within
- Reference features of OS Boundaryline (selected features only)
- Press OK
- In the Results box, press the Create Layer from Selected to create a new layer
- Right click on the newly added Selection from Roads layer in the Layer Panel
- Select Query
- Use the Query Builder to enter the following SQL “Classification” = ‘A Road’ OR “Classification” = ‘Motorway’
- Press OK